package com.ls.x.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ls.x.R;
import com.ls.x.activity.beans.UserBean;
import com.ls.x.service.IUserService;
import com.ls.x.service.UserService;
import com.ls.x.tool.DatabaseHelper;
import com.ls.x.tool.EncryptTool;

public class LoginActivity extends Activity {

	private DatabaseHelper dbHelper = null;
	private EditText edt_username;
	private EditText edt_password;
	private String username;
	private String password;
	private SharedPreferences preferences;
	private SharedPreferences.Editor editor;
	private String pre_username;
	private String pre_password;
	private IUserService userService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		dbHelper = DatabaseHelper.getDBhelper(LoginActivity.this);
		userService = new UserService(dbHelper);

		preferences = getSharedPreferences("login", MODE_WORLD_READABLE);
		editor = preferences.edit();
		pre_username = preferences.getString("username", "");
		if (!pre_username.equals("")) {
			pre_password = preferences.getString("password", "");
			if (login(pre_username, pre_password)) {
				onDestroy();
			}
		}
		setContentView(R.layout.page_login);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

		edt_username = (EditText) findViewById(R.id.edt_login_page_username);
		edt_password = (EditText) findViewById(R.id.edt_login_page_password);

		Button btnLogon = (Button) findViewById(R.id.btn_login_page_logon);
		btnLogon.setOnClickListener(new LogonButtonOnClickListener());
		Button btnRegister = (Button) findViewById(R.id.btn_login_page_reg);
		btnRegister.setOnClickListener(new RegisterButtonOnClickListener());

		// 该按钮仅做为测试使用-用来删除数据库中已有用户。
		Button btnTestDelete = (Button) findViewById(R.id.btn_login_page_testdelete);
		btnTestDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 删除测试用户
				userService.deleteAllUser();
				// db.execSQL("delete from user");
				Toast.makeText(LoginActivity.this, "删除所有用户！", 500).show();

			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#finish()
	 */
	@Override
	public void finish() {
		super.finish();
	}

	protected void onDestroy() {
		super.onDestroy();
		Log.d("lijing", "LoginActivity进入onDestroy()");
		super.onStop();
		if (dbHelper != null) {
			dbHelper.close();
		}
	}

	public boolean isUserExist(String username) {
		UserBean userBean = new UserBean();
		userBean.setUsername(username);
		List<UserBean> userBeanList = userService
				.selectUserByUserBean(userBean);

		if (userBeanList.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean checkUserPassword(String username, String password) {
		
		UserBean userBean = new UserBean();
		userBean.setUsername(username);
		userBean.setPassword(password);
		List<UserBean> userBeanList = userService
				.selectUserByUserBean(userBean);
		
		if (userBeanList.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean login(String uname, String passwd) {
		if (isUserExist(uname)) {
			if (checkUserPassword(uname, passwd)) {
				Toast.makeText(LoginActivity.this, "登录成功！", 500).show();
				Intent intent = new Intent();
				intent.setClass(LoginActivity.this, IndexActivity.class);
				startActivity(intent);
				onDestroy();
				finish();
				return true;
			} else {
				Toast.makeText(LoginActivity.this, "用户名或密码不正确！", 500).show();
				edt_password.setText("");
			}

		} else {
			if (pre_username.equals("")) {
				Toast.makeText(LoginActivity.this, "该用户不存在", 500).show();
			}
		}
		return false;
	}

	class LogonButtonOnClickListener implements OnClickListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.view.View.OnClickListener#onClick(android.view.View)
		 */
		@Override
		public void onClick(View v) {
			username = edt_username.getText().toString();
			password = EncryptTool.getMD5Str(edt_password.getText().toString());
			if (login(username, password)) {
				editor.putString("username", username);
				editor.putString("password", password);
				editor.commit();
				onDestroy();
				finish();
			}
		}

	}

	class RegisterButtonOnClickListener implements OnClickListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.view.View.OnClickListener#onClick(android.view.View)
		 */
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			username = edt_username.getText().toString();
			if (!username.equals("")) {
				intent.putExtra("username", username);
			}
			intent.setClass(LoginActivity.this, RegisterActivity.class);
			startActivity(intent);
		}
	}

}
